﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Util;

namespace Negro
{
    public partial class Agregar : Util.Action
    {
        public Tabla tabla = null;

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                this.consulta();
            }
            else
            {
                this.tabla = (Tabla)Session["TABLA"];

            }
        }
        
        protected void btnAceptar_Click(object sender, EventArgs e)
        {

            AgregarBusiness business = new AgregarBusiness();

            productos bean = new productos();
            bean.descripcion = this.descripcion.Text;
            bean.nombre = this.nombre.Text;
            bean.existencia = System.Convert.ToDecimal(this.existencia.Text);
            bean.precioCompra = System.Convert.ToDecimal(this.precioCompra.Text);
            bean.precioVenta = System.Convert.ToDecimal(this.precioVenta.Text);

            business.guardar(bean);

            this.consulta();

        }


        private void consulta()
        {
            AgregarBusiness business = new AgregarBusiness();

            List<ProductosDTO> list = business.consulta().ToList<ProductosDTO>();

            ArrayList arrayList = new ArrayList(list);

            DataTable dataTable = Util.Convert.ToDataTable<ProductosDTO>(list);

            this.lista.DataSource = dataTable;

            this.lista.DataBind();

            this.tabla = new Tabla(arrayList, dataTable);

            Session["TABLA"] = tabla;

        }

        protected void lista_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {

            this.lista.DataSource = this.tabla.DataTable;

            this.pageIndexChanging(this.lista, e);
        }

        protected void lista_Sorting(object sender, GridViewSortEventArgs e)
        {

            this.lista.DataSource = this.tabla.DataTable;

            this.sorting(this.lista, e);
        }


        protected void CheckBoxAll_CheckedChanged(object sender, EventArgs e)
        {

            GridViewRow headerRow = this.lista.HeaderRow;

            CheckBox obj = (CheckBox)headerRow.FindControl("CheckBoxAll");

            Boolean Checked = obj.Checked;


            //Boolean Checked = this.CheckBoxAlls.Checked;


            this.tabla.Checkeds[0] = Checked;

            if (this.tabla.List.Count > 0)
            {
                this.tabla.checkAllList();

                DataTable dataTable = Util.Convert.ToDataTable<ProductosDTO>(this.tabla.List);

                this.tabla.DataTable = dataTable;

                this.lista.DataSource = this.tabla.DataTable;

                this.lista.DataBind();
            }

            Session["TABLA"] = this.tabla;
        }

        protected void btnActualizar_Click(object sender, EventArgs e)
        {

            this.tabla = (Tabla)Session["TABLA"];

            int index = 0;

            foreach (GridViewRow item in this.lista.Rows)
            {

                CheckBox obj = (CheckBox)item.FindControl("txtCheck");

                Boolean valor = obj.Checked;

                ProductosDTO dto = (ProductosDTO)this.tabla.List[index];

                dto.Check = valor;

                index++;

            }

        }

        protected void btnRegresar_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/aspx/Menu.aspx");
        }

    }
       
}